<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script>
    /*
      生成器函数“
      1.function后边会跟上*
      2.生成器函数中的代码执行可以被yield控制
      3.生成器函数默认在执行时，返回一个生成器对象


      *想要执行函数中的代码，需要先获取到生成器对象，调用它的next方法
      *当遇到yeild时，就会中断执行
    */

    function* foo() {
      console.log(111);
      console.log(222);
      let name2 = yield "aaa"
      console.log(333, name2);
      console.log(4444, name2);
      return "bbbb"
      // yield "bbbb"
      console.log(555);
      yield "ccc"
      return undefined
    }


    // 生成器函数会返回一个生成器对象
    let generator = foo()
    // generator.next()
    // generator.next()
    // 1. 中间没有return
    // console.log(generator.next()); // {done:false,vlaue:'aaa'}
    // console.log(generator.next()); // {done:false,vlaue: 'bbb'}
    // console.log(generator.next()); // {done:false,vlaue: 'ccc'}
    // console.log(generator.next()); // {done:true,vlaue:  undefined}


    // 2. 当中间有return的时候
    console.log(generator.next()); // {done:false,vlaue:'aaa'}
    console.log(generator.next('你好')); // {done:true,vlaue:'bbb'}
    console.log(generator.next()); // {done:true,vlaue:undefine}
    console.log(generator.next()); // {done:true,vlaue:undefine}
    console.log(generator.next()); // {done:true,vlaue:undefine}
    console.log(generator.next()); // {done:true,vlaue:undefine}
    console.log(generator.next()); // {done:true,vlaue:undefine}
    console.log(generator.next()); // {done:true,vlaue:undefine}
    console.log(generator.next()); // {done:true,vlaue:undefine}
    console.log(generator.next()); // {done:true,vlaue:undefine}
    console.log(generator.next()); // {done:true,vlaue:undefine}

    // 3.给函数每次执行的时候传入参数
    // console.log(generator.next()); //第一次调用next一般是不传参的，要传的话直接在函数调用的时候传入
    // console.log(generator.next("第二次执行的时候传入的参数字符串"));

  </script>

</body>

</html>